package com.aistar.util;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

//封装了获得SqlSession对象与关闭SqlSession
public class MyBatisUtil {

    //JDBC PreparedStatement sql
    //Mybatis SqlSession.insert|delete|select|update

    //SqlSessionFactory 重量级的对象
    private static SqlSessionFactory sqlSessionFactory ;
    private static SqlSession sqlSession ;

    static {
        InputStream is = MyBatisUtil.class.getClassLoader().getResourceAsStream("config/mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    }

    public static SqlSession getSqlSession(){
        sqlSession = sqlSessionFactory.openSession();//getConnection()
        return  sqlSession;
    }


    public static void closeSqlSession(){
         if(sqlSession !=null)
             sqlSession.close();
    }
}
